package org.ghost.http.testkit.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.ghost.http.testkit.common.errorcode.SystemErrorCodeEnum;
import org.ghost.http.testkit.common.exception.CoreException;
import org.ghost.http.testkit.dto.ResponseVO;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

/**
 * Created by lykosliu on 2017/3/22.
 */
@Aspect
@Configuration
public class AspectConfig {
    @Pointcut("execution(* org.ghost.http.testkit.controller.*Controller.*(..))")
    public void executeController() {
    }

    /**
     * 拦截器具体实现
     *
     * @param pjp
     * @return JsonResult（被拦截方法的执行结果，或需要登录的错误提示。）
     */
    @Around("executeController()") //指定拦截器规则；也可以直接把“execution(* com.xjj.........)”写进这里
    public Object Interceptor(ProceedingJoinPoint pjp) {
        Object result = null;
        try {
            result = pjp.proceed();
        } catch (IllegalArgumentException ia) {
            result = new ResponseVO(SystemErrorCodeEnum.PARAM_ERROR.getCode(), ia.getMessage());
        } catch (CoreException qe) {
            result = new ResponseVO(qe.getCode(), qe.getMessage());
        } catch (Throwable t) {
            result = new ResponseVO(SystemErrorCodeEnum.SYSTEM_ERROR.getCode(), SystemErrorCodeEnum.SYSTEM_ERROR.getMessage() + (t.getMessage() == null ? "" : t.getMessage()));
        }
        return result;
    }


    @Bean("messageSource")
    public ReloadableResourceBundleMessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setCacheSeconds(1);
        messageSource.setBasename("classpath:/i18n/message");
        return messageSource;
    }

}
